package com.fan.teamer.process;

import org.apache.log4j.Logger;

import com.fan.teamer.global.TeamerGlobalVariables;
import com.fan.teamer.model.domain.Attendance;
import com.fan.teamer.model.domain.Event;
import com.fan.teamer.model.domain.Member;
import com.fan.teamer.model.domain.Team;
import com.fan.teamer.service.AttendanceService;
import com.fan.teamer.service.EventService;
import com.fan.teamer.service.MemberService;
import com.fan.teamer.service.TeamService;

public class MemberQuitEventProcess extends ProcExec<Integer> {

	private static Logger logger = Logger.getLogger("MemberQuitEventProcess");

	private Member member;
	private Event event;
	private MemberService memberService;
	private TeamService teamService;
	private EventService eventService;
	private AttendanceService attendanceService;

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		MemberQuitEventProcess.logger = logger;
	}

	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public Event getEvent() {
		return event;
	}

	public void setEvent(Event event) {
		this.event = event;
	}

	public MemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public TeamService getTeamService() {
		return teamService;
	}

	public void setTeamService(TeamService teamService) {
		this.teamService = teamService;
	}

	public EventService getEventService() {
		return eventService;
	}

	public void setEventService(EventService eventService) {
		this.eventService = eventService;
	}

	public AttendanceService getAttendanceService() {
		return attendanceService;
	}

	public void setAttendanceService(AttendanceService attendanceService) {
		this.attendanceService = attendanceService;
	}

	public int initCheck() {
		if (teamService == null || memberService == null
				|| eventService == null || attendanceService == null) {
			setErrorMessage("Service is null!");
			return TeamerGlobalVariables.FAILED;
		}
		if (member == null || event == null) {
			setErrorMessage("Domain cannot be null!");
			return TeamerGlobalVariables.FAILED;
		}

		if (!memberService.hasMember(member.getEmail())) {
			setErrorMessage("Member: " + member.getEmail() + " doesn't exist!");
			return TeamerGlobalVariables.FAILED;
		}
		if (!eventService.hasEvent(event.getId())) {
			setErrorMessage("Event: " + event.getEventName()
					+ " doesn't exist!");
			return TeamerGlobalVariables.FAILED;
		}

		Team team = teamService.getTeam(event.getTeamId());

		if (!teamService.hasMember(team, member)) {
			setErrorMessage("Team " + team.getName() + " doesn't have user: "
					+ member.getEmail());
			return TeamerGlobalVariables.FAILED;
		}

		return TeamerGlobalVariables.SUCCESS;
	}

	@Override
	public Integer processImpl() {
		if (initCheck() == TeamerGlobalVariables.FAILED) {
			return TeamerGlobalVariables.FAILED;
		}
		Attendance attendance = attendanceService.getAttendance(event, member);
		if (attendance == null) {
			setErrorMessage("Member " + member.getEmail()
					+ " not joined Event " + event.getEventName() + " before!");
			return TeamerGlobalVariables.FAILED;
		}

		try {
			attendanceService.deleteAttendance(attendance);
		} catch (Exception e) {
			e.printStackTrace();
			setErrorMessage("Member " + member.getEmail()
					+ " cannot quit from event " + event.getEventName());
			return TeamerGlobalVariables.FAILED;
		}

		return TeamerGlobalVariables.SUCCESS;
	}

}
